package com.xinhe.web.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.google.common.collect.Maps;
import com.xinhe.web.entity.WeixinConfig;
import com.xinhe.web.enums.ConfigType;
import com.xinhe.web.reposity.ValueConfigRepository;
import com.xinhe.web.reposity.WeixinConfigRepository;
import me.chanjar.weixin.common.service.WxService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
public class WxConfigManager {
    private final Map<ConfigType, WxService> mpServices = Maps.newHashMap();
    @Autowired
    private WeixinConfigRepository weixinConfigRepository;

    public WxMpDefaultConfigImpl getWxMpConfigStorage() {
        return this.wxMpConfigStorage;
    }

    private WxMpDefaultConfigImpl wxMpConfigStorage;
    @Autowired
    private ValueConfigRepository valueConfigRepository;

    public void init() {
        Long weixinConfigIdService = Long.valueOf(valueConfigRepository.findById("weixin_config_id_service").get().getValue());
        WeixinConfig wxConfig = this.weixinConfigRepository.findById(weixinConfigIdService).get();
        WxMpServiceHttpClientImpl wxMpServiceHttpClientImpl = new WxMpServiceHttpClientImpl();
        this.wxMpConfigStorage = new WxMpDefaultConfigImpl();
        this.wxMpConfigStorage.setToken("xqg1024");
        this.wxMpConfigStorage.setAesKey("q8CkRv718wJ23rFh7VxfeJ7xFP10k7KKsWZ13p8f5CV");
        this.wxMpConfigStorage.setAppId(wxConfig.getAppId());
        this.wxMpConfigStorage.setSecret(wxConfig.getAppSecret());
        wxMpServiceHttpClientImpl.setWxMpConfigStorage((WxMpConfigStorage) this.wxMpConfigStorage);
        this.mpServices.put(com.xinhe.web.enums.ConfigType.MAP, wxMpServiceHttpClientImpl);

        Long weixin_config_id_mini_program = Long.valueOf(valueConfigRepository.findById("weixin_config_id_mini_program").get().getValue());
        wxConfig = this.weixinConfigRepository.findById(weixin_config_id_mini_program).get();
        WxMaServiceImpl wxMaServiceImpl = new WxMaServiceImpl();
        WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
        wxMaConfig.setAppid(wxConfig.getAppId());
        wxMaConfig.setSecret(wxConfig.getAppSecret());
        wxMaServiceImpl.setWxMaConfig(wxMaConfig);
        this.mpServices.put(ConfigType.MINI_PROGRAM, wxMaServiceImpl);
    }

    public WxMpService getWxMpService() {
        return (WxMpService) this.mpServices.get(ConfigType.MAP);
    }

    public WxMaService getWxMiniProgramService() {
        return (WxMaService) this.mpServices.get(ConfigType.MINI_PROGRAM);
    }
}
